package control.utils;

import java.awt.AWTException;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.image.BufferedImage;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.imageio.ImageIO;
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Part;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.swing.ImageIcon;

public class Util {
	
	public static void sendMail(String smtp, String subject, String[] to,
			String from, String message,String user,String password) {
		Transport trans =null;
		try {
			System.out.println("Enviando E-mail");
			Properties p = new Properties();
			p.put("mail.host", smtp);
			p.put("mail.smtp.starttls.enable","true");
			Session session = Session.getInstance(p, null);
			MimeMessage msg = new MimeMessage(session);

			// "de" e "para"!!
			msg.setFrom(new InternetAddress(from));
			
			final Address[] recAdd = InternetAddress.parse(to[0]+","+to[1]);
			
			msg.setRecipients(Message.RecipientType.TO, recAdd);

			// nao esqueca da data!
			// ou ira 31/12/1969 !!!
			msg.setSentDate(new Date());

			msg.setSubject("Ping Falhando");

			msg.setText(message);

			
			trans= session.getTransport("smtps");
			trans.connect("smtp.gmail.com", user, password);
			trans.sendMessage(msg, recAdd);
			// evniando mensagem (tentando)
			trans.close();
			// ////////////////////
			System.out.println("E-mail enviado com sucesso");
		}

		catch (AddressException adrEx) {
			if(trans.isConnected())
				try {
					trans.close();
				} catch (MessagingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			throw new IllegalArgumentException(
					"AddressException: Cannot send message.\n"
							+ adrEx.getMessage());
		} catch (MessagingException msgEx) {
			if(trans.isConnected())
				try {
					trans.close();
				} catch (MessagingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			throw new IllegalArgumentException(
					"MessagingException: Cannot send message.\n"
							+ msgEx.getMessage());
		}
	}


	public static String getTimeToString(Date time,String formatString)
	{
		try
		{
			SimpleDateFormat format = new SimpleDateFormat(formatString);
			return format.format(time);
		}
		catch (Exception ex)
		{
			return "";
		}
	}
}
